package com.jz.community.moduleshoppingguide.search.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.RegionsBean;
import com.jz.community.basecomm.bean.baseGoods.BaseGoodsInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.BaseUserUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.moduleshoppingguide.search.bean.SearchGoodsInfo;
import com.jz.community.moduleshoppingguide.search.bean.SearchHotKeyWordInfo;

import java.util.HashMap;

/**
 * Created by Tony on 2018/6/05
 */
public class SearchGoodsListTask extends RxTask<String, Integer, BaseGoodsInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public SearchGoodsListTask(Activity activity,
                               ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }

    @Override
    protected BaseGoodsInfo doInBackground(String... params) {
        // 执行请求
        String result = OkHttpUtil.get(Constant.SHOW_GOODS_INFOES, getParam(params[0], params[1], params[2]));
        BaseGoodsInfo info = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            info = JsonUtils.parseObject(result, BaseGoodsInfo.class);
        }
        return info;
    }

    @Override
    protected void onPostExecute(BaseGoodsInfo result) {
        // 执行结果回调函数
        taskListener.doTaskComplete(result);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    /**
     * 获取请求所需的参数
     *
     * @param
     * @return
     */
    private HashMap<String, Object> getParam(String title, String sort, String page) {
        HashMap<String, Object> param = new HashMap<>();
        RegionsBean.EmbeddedBean.ContentBean contentBean = BaseSpUtils.getInstance().getRegion(activity);
        param.put("filter", "shopType::1" + "|" + "title::" + title + "|citycode::" + contentBean.getCode());
        if ("0".equals(sort)) {
            param.put("sort", "priceasc");
        } else {
            param.put("sort", "pricedesc");
        }
        param.put("page", page);
        param.put("size", 10);
        return param;
    }
}
